package com.wxd.framework.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.Criteria;
import com.wxd.framework.common.page.Pagination;
import com.wxd.framework.dao.MenuDao;
import com.wxd.framework.dao.RoleDao;
import com.wxd.framework.dao.RoleMenuDao;
import com.wxd.framework.dao.UserDao;
import com.wxd.framework.entity.Menu;
import com.wxd.framework.entity.Role;
import com.wxd.framework.entity.RoleMenu;
import com.wxd.framework.entity.User;
import com.wxd.framework.service.RoleService;
import com.wxd.framework.vo.TreeItem;

@Service
public class RoleServiceImpl extends BaseServiceImpl implements RoleService{

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private RoleMenuDao roleMenuDao;
	
	/* (non-Javadoc)
	 * @see com.wxd.framework.service.RoleService#stopOneRole(java.lang.String, boolean, java.lang.String)
	 */
	@Override
	public void stopOneRole(String id, boolean isStoped, String updater) {
		Role role = new Role();
		role.setId(id);
		role.setIsStoped(!isStoped);
		role.setUpdater(updater);
		role.setUpdateTime(new Date());
		roleDao.updateSelective(role);
	}

	/* (non-Javadoc)
	 * @see com.wxd.framework.service.RoleService#getMenuTreeByRoleId(java.lang.String)
	 */
	@Override
	public List<TreeItem> getMenuTreeByRoleId(String roleId) {
		List<TreeItem> tree = new ArrayList<>();
		//获得一级菜单
		List<Menu> firstLevelMenus = menuDao.selectByParentId("0");
		//获取当前角色的关联菜单
		List<RoleMenu> roleMenus = roleMenuDao.selectByRoleId(roleId);
		
		for(Menu menu : firstLevelMenus){
			TreeItem item = new TreeItem(menu);
			//初始化一级菜单 
/*			for(RoleMenu roleMenu : roleMenus){
				if (roleMenu.getMenuId().equals(item.getId())) {
					item.setChecked(true);
					break;
				}
			}*/
			//初始化二级菜单
			List<Menu> secondLevelMenus = menuDao.selectByParentId(menu.getId());
			List<TreeItem> childrens = new ArrayList<>();
			for(Menu secondMenu : secondLevelMenus){
				TreeItem item2 = new TreeItem(secondMenu);
				for(RoleMenu roleMenu : roleMenus){
					if (roleMenu.getMenuId().equals(item2.getId())) {
						item2.setChecked(true);
						break;
					}
				}
				childrens.add(item2);
			}
			item.setChildren(childrens);
			tree.add(item);			
		}
		return tree;
	}

	/* (non-Javadoc)
	 * @see com.wxd.framework.service.RoleService#saveRoleMenuForRole(java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void saveRoleMenuForRole(String roleId, String menuIds) {
		//清空该角色的所有菜单权限
		roleMenuDao.deleteByRoleId(roleId);
		String[] menuIdArray = menuIds.split(FrameConstants.NORMAL_FLAG);
		for(String menuId : menuIdArray){
			if(StringUtils.isNotBlank(menuId)){
				RoleMenu roleMenu = new RoleMenu();
				roleMenu.setRoleId(roleId);
				roleMenu.setMenuId(menuId);
				roleMenuDao.insertSelective(roleMenu);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.wxd.framework.service.RoleService#getUsersByRoleId(java.lang.String)
	 */
	@Override
	public List<User> getUsersByRoleId(String roleId) {
		List<User> list = userDao.selectByRoleId(roleId);
		return list;
	}

	@Override
	public Role addOneRole(Role role) {
		List<Role> list = roleDao.selectByRoleName(role.getRoleName());
		if (list != null && list.size() > 0) {
			role.setMessage("角色已存在");
		}else{
			roleDao.insertSelective(role);
		}		
		return role;
	}

	@Override
	public void deleteOneRole(String id) {
		roleDao.deleteByPrimaryKey(id);	
	}

	@Override
	public void editOneRole(Role role) {
		roleDao.updateSelective(role);
	}

	@Override
	public Role getOneRoleById(String id) {
		return roleDao.selectByPrimaryKey(id);
	}

	@Override
	public Pagination getRolesInPage(Criteria criteria) {
		return roleDao.getPaginationByCriteria(criteria);
	}

}
